package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.DateTimeUtils;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.domain.SysUser;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import com.yzkj.cpjxc.business.domain.ZhangTao;
import com.yzkj.cpjxc.business.domain.ZhangTaoQueryVo;
import com.yzkj.cpjxc.business.service.ZhangTaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 账套控制器
 */
@Controller
@RequestMapping("/zhangTao")
public class ZhangTaoController extends BaseController {

	@Autowired
	private ZhangTaoService zhangTaoService;

	@RequestMapping("/list")
	@DoLog(cnContent = "查看账套列表信息")
	@Permission(name="zhangTao.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("zhangTao", zhangTao);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("zhangTao/list").addAllObjects(modelMap);
	}

	@RequestMapping("/ajaxPage")
	@DoLog(cnContent = "账套列表ajax分页")
	@Permission(name="zhangTao.view")
	public String ajaxPage(HttpServletRequest request, HttpServletResponse response, ZhangTaoQueryVo queryVo) throws Exception {
		try {
			Pager<ZhangTao> p = new Pager<ZhangTao>();
			String currentPage = request.getParameter(Pager.MY_PAGINATION_CURRENT_PAGE);
			p.setCurrentPage(Integer.parseInt(currentPage == null ? "1" : currentPage));

			SysUser sysUser = (SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
			queryVo.setUserId(sysUser.getUserId());

			Pager<ZhangTao> pager = zhangTaoService.search(p, queryVo);
			this.genPageJson(response, pager);
			return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toAdd")
	@DoLog(cnContent = "去到添加账套页面")
	@Permission(name="zhangTao.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("maxTime", DateTimeUtils.dateToString(new Date(), DateTimeUtils.sdfyyyyMMdd));
		return new ModelAndView("zhangTao/add").addAllObjects(modelMap);
	}

	@RequestMapping("/add")
	@DoLog(cnContent = "添加账套")
	@Permission(name="zhangTao.add")
	public String add(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		try {
			Integer qyYear = zhangTao.getQyYear();
			Integer qyMonth = zhangTao.getQyMonth();
			if(qyYear==null) {
				return this.ajax(response, "启用年份不能为空！");
			}
			if(qyYear<1900) {
				return this.ajax(response, "启用年份不能小于1900年！");
			}
			if(qyYear>DateTimeUtils.getYear()) {
				return this.ajax(response, "启用年份不能大于当前年份"+DateTimeUtils.getYear()+"年！");
			}
			
			if(qyMonth==null) {
				return this.ajax(response, "启用月份不能为空！");
			}
			if(qyMonth<1||qyMonth>12) {
				return this.ajax(response, "启用月份必须大于等于0并且小于等于12的正整数！");
			}
			
			SysUser sysUser = (SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
			Date date = new Date();
			zhangTao.setUuid(IDUtils.genUUID());
			zhangTao.setCreateTime(date);
			zhangTao.setUpdateTime(date);
			zhangTao.setIsActive(Constants.ACTIVE);
			zhangTao.setUserId(sysUser.getUserId());

			BusinessMap bm = zhangTaoService.create(zhangTao);

			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toEdit")
	@DoLog(cnContent = "去到编辑账套页面")
	@Permission(name="zhangTao.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		ZhangTao result = zhangTaoService.getById(zhangTao.getUuid());
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("zhangTao", result);
		return new ModelAndView("zhangTao/edit").addAllObjects(modelMap);
	}

	@RequestMapping("/edit")
	@DoLog(cnContent = "编辑账套")
	@Permission(name="zhangTao.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		try {

			Date date = new Date();
			zhangTao.setUpdateTime(date);
			
			zhangTao.setQyYear(null);
			zhangTao.setQyMonth(null);
			zhangTao.setBwb(null);
			zhangTao.setKzzd(null);

			BusinessMap bm = zhangTaoService.edit(zhangTao);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/view")
	@DoLog(cnContent = "查看账套信息")
	@Permission(name="zhangTao.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		ZhangTao result = zhangTaoService.getById(zhangTao.getUuid());
		return new ModelAndView("zhangTao/view").addObject("zhangTao", result);
	}

	@RequestMapping("/delete")
	@DoLog(cnContent = "删除账套信息")
	@Permission(name="zhangTao.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, ZhangTao zhangTao) throws Exception {
		try {
			boolean flag = zhangTaoService.delete(zhangTao.getUuid());
			return this.ajax(response, flag ? Constants.SUCCESS : Constants.ERROR);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

}
